<?php
require_once(dirname(__FILE__)."/../../config/session.php");
require_once(dirname(__FILE__)."/../../libs/dbclass/dbconnect.php");
include_once(dirname(__FILE__)."/../../libs/string/libstr.php");
$mc= new libstr();
class login{
//管理员登录
function admin_login($userid,$password,$code=""){
	global $mc;
	global $db;
	if($mc->Ismp($userid)=="" ){
		return "userid is null";//账号为空
	 }
	 
	 if($mc->Ismp($password)=="" ){
		return "password is null";//密码为空
	 }
	 
	if($userid!="" && $password!=""){
		
		$mb=$db->ds("select userid,password,userno,username,rootid from member_content where userid='".trim($userid)."'");
		if(count($mb)==0){
		 return "userid is wrong";//账号错误 
		}
		
		if(strtoupper(md5(trim($password)))!=$mb[0]["password"]){	
		return 	"password is wrong";//密码错误
		}
	
		$ip= $mc->getip();
		$time=date('Y-m-d H:i:s',time());
		//$db->cmd("update member_content set logintime='".$time."',hit=hit+1 ,loginip='".$ip."' where userno=".$mb[0]["userno"]);//更新登录信息
		//$db->cmd("insert into user_login (ip,createtime,userno) values('".$ip."','".$time."','".$mb[0]["userno"]."')");//记录登录信息
		return "login is ok|userno=".$mb[0]["userno"];//登录成功
	}
}


//获得用户权限和菜单
function getpower($userno){
	global $mc;
	global $db;
	if($userno==""){
		return "";
	}
	$pp=$db->ds("select pw.quanxian as qx,pw.caidan as cd from power_class as pw where ID=(select mb.quanxian from member_class as mb where mb.rootid=(select mbc.rootid from member_content as mbc where userno='".$userno."' limit 1) limit 1)");
	return $pp[0];
}


/** 
* ispower
* 判断权限
* @param 字符类型 $qx 用户的权限
* @param 数字类型 $pwid 某项权限ID
* @return  布尔值
*  */
function ispower($qx,$pwid){
  global $mc;
	  
  if($mc->Ismp($qx)==""){
	  return false;
  }
  $pw=split(",",$qx);
  
  if(in_array($pwid,$pw)){
  	return true;	
  }else{
	  return false;
  }
}

/** 
* checklogin
* 检验是否登录
* @param 字符类型 $usertype 用户的类型
* @param 字符类型 $qx 用户的权限
* @param 数字类型 $pwid 某项权限ID
* @return  布尔值
* */
function checklogin($usertype="admin_member",$sesionname="userid",$go="",$tx=""){
	global $mc;
	if($mc->Ismp($_SESSION[$usertype][$sesionname])=="")
	{
		if($go!=""){
			$mc->msg($tx,$go);
		}else{
			if($usertype=="admin_member"){
			   echo "<script>top.location.href='/admin/login.php'</script>";		
			}else{
			   echo "<script>top.location.href='/login.php'</script>";		
			}
		}
		
	}
}
	
//用户登录
function member_login($userid,$password){
	global $mc;
	global $db;
		
		
	if($mc->Ismp($userid)=="" ){
		return "userid is null";//账号为空
	 }
	 if($mc->Ismp($password)=="" ){
		return "password is null";//密码为空
	 }
	 
	if($userid!="" && $password!=""){	
		$mb=$db->ds("select userid,password,userno,username,rootid from member_content where userid='".trim($userid)."'");
		if(count($mb)==0){
			return "userid is wrong";//账号错误 
		}
		
		if(strtoupper(md5(trim($password)))!=$mb[0]["password"]){	
			return 	"password is wrong";//密码错误
		}
	
		$ip= $mc->getip();
		$time=date('Y-m-d H:i:s',time());
		$db->cmd("update member_content set logintime='".$time."',hit=hit+1 ,loginip='".$ip."' where userno=".$mb[0]["userno"]);//更新登录信息
		$db->cmd("insert into user_login (ip,createtime,userno) values('".$ip."','".$time."','".$mb[0]["userno"]."')");//记录登录信息
		return "login is ok";//登录成功
	}
}

/*获得用户详细*/
function getuserinfo($userno)
{
  global $db,$mc;
  if($mc->Ismp($userno)==""){
  	return "userno is null";	
  }
  $getrs=$db->dvstr("select * from member_content where userno=".$userno."");
  return $getrs;
}

//转化身份类别
function getclassname($rootid){
  global $mc,$db;
  $cc=$db->dvstr("select * from member_class where rootid='".$rootid."'");
  return $cc["classname"];
}

//获取session
function getsession($name1="admin_member",$name2="userno"){
  global $mc,$db;
  if($mc->Ismp($_SESSION[$name1])!=""){
	  if($name2!=""){
	  	return 	 $_SESSION[$name1][$name2];
	  }	
  }
  return "";
}		
	
/*修改用户密码*/
function update_psw($userno,$password,$newpassword){
	global $db,$mc;
	if($mc->Ismp($userno)=="" || $mc->Ismp($password)==""){
		return "userno or password is null";
	}
	$mb=$db->ds("select userid,password,userno from member_content where userno='".trim($userno)."'");
	
	if(count($mb)==0){
		 return "userno is wrong";//账号错误 
	}
	
	if(strtoupper(md5(trim($password)))!=$mb[0]["password"]){	
		return 	"password is wrong";//密码错误
	}
	
  if($db->cmd("update member_content set password='".strtoupper(md5(trim($newpassword)))."' where userno=".$mb[0]["userno"])){
	return "update is ok";
  }	
}
	///*修改用户邮箱*/
	//function update_email($userid,$password,$newemail){
	//		global $db,$mc;
	//	if($mc->Ismp($userid)=="" || $mc->Ismp($password)==""){
	//		return "userid or password is null";
	//}
	//if($mc->Ismp($newemail)==""){
	//	return "newemail is null";
	//	
	//}
	//$mb=$db->ds("select userid,password,userno,username,rootid,shenfenid,jigou_userno from member_content where userid='".trim($userid)."'");
	//	if(count($mb)==0)
	//	{
	//	 return "userid is wrong";//账号错误
	//     
	//	}
	//	if(strtoupper(md5(trim($password)))!=$mb[0]["password"]){
	//		
	//	return 	"password is wrong";//密码错误
	//	}
	//if($db->cmd("update member_content set email='".trim($newemail)."' where userno=".$mb[0]["userno"])){
	//	
	//	return "update is ok";
	//}	
	//	
	//}

}

?>
